package com.xx.bysj.controller;

import com.xx.bysj.dto.HnInformation;
import com.xx.bysj.dto.HnMenu;
import com.xx.bysj.dto.HnOrder;
import com.xx.bysj.dto.HnOrderInfo;
import com.xx.bysj.dto.HnOrderPhoto;
import com.xx.bysj.dto.HnRole;
import com.xx.bysj.dto.HnUser;
import com.xx.bysj.mapper.HnInformationMapper;
import com.xx.bysj.mapper.HnMenuMapper;
import com.xx.bysj.mapper.HnOrderInfoMapper;
import com.xx.bysj.mapper.HnOrderMapper;
import com.xx.bysj.mapper.HnOrderPhotoMapper;
import com.xx.bysj.mapper.HnRoleMapper;
import com.xx.bysj.mapper.HnUserMapper;
import com.xx.bysj.mapper.MenuMapper;
import com.xx.bysj.service.UserService;
import javafx.scene.input.DataFormat;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 基础跳转功能类
 */
@Controller
public class RedirectController {

    @Autowired
    private HnMenuMapper menuMapper;

    @Autowired
    private HnInformationMapper informationMapper;

    @Autowired
    private HnUserMapper userMapper;

    @Autowired
    private HnRoleMapper roleMapper;

    @Autowired
    private HnOrderMapper orderMapper;

    @Autowired
    private HnOrderInfoMapper orderInfoMapper;

    @Autowired
    private HnOrderPhotoMapper photoMapper;

    @GetMapping("/securityLogin")
    public String login() {
        return "login";
    }

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/securityRegister")
    public String register() {
        return "register";
    }

    @GetMapping("/welcome")
    public String welcome(Map<String,Object> map, Authentication authentication) {
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            information.setAlert(false);
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setAlert(true);
            map.put("information",information);
        }
        return "welcome";
    }

    @GetMapping("/thisMenu")
    public String menu() {
        return "menuPage/menu";
    }

    @GetMapping("/icon")
    public String icon() {
        return "system/icon";
    }

    @GetMapping("/table")
    public String table() {
        return "system/table";
    }

    @GetMapping("/form")
    public String form() {
        return "system/form";
    }

    @GetMapping("/button")
    public String button() {
        return "system/button";
    }

    @GetMapping("/error404")
    public String error404() {
        return "error/404";
    }

    @GetMapping("/error500")
    public String error500() {
        return "error/500";
    }

    @GetMapping("/menuAdd")
    public String menuAdd() {
        return "menuPage/menuAdd";
    }

    @GetMapping("/menuAudit")
    public String edit(Map<String, Object> map, String authorityName) {
        //通过Map<String, Object> map的方式可以将自己需要的数据存储并返回给需要的页面来进行调用(经典前后端不分离方法)
        if (StringUtils.isNotBlank(authorityName)) {
            List<HnMenu> menu = menuMapper.selectByTitle(authorityName);
            if (CollectionUtils.isNotEmpty(menu)) {
                map.put("menu", menu.get(0));
            } else {
                map.put("menu", null);
            }
        } else {
            map.put("menu", null);
        }
        return "menuPage/menuEdit";
    }

    @GetMapping("/userList")
    public String userList() {
        return "users/userList";
    }

    @GetMapping("/userSetting")
    public String userSetting(Map<String, Object> map, Authentication authentication) {
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("user",thisUser);
        return "users/userSetting";
    }
    @GetMapping("/userEdit")
    public String userAdd() {
        return "users/userEdit";
    }

    @GetMapping("/roleList")
    public String roleList() {
        return "role/roleList";
    }

    @GetMapping("/roleAdd")
    public String roleAdd() {
        return "role/roleAdd";
    }

    @GetMapping("/roleEdit")
    public String roleEdit(Map<String, Object> map, Integer roleId){
        HnRole role = roleMapper.selectByPrimaryKey(roleId);
        if (null != role) {
            map.put("role",role);
        }else{
            role = new HnRole();
            map.put("role",role);
        }
        return "role/roleEdit";
    }

    @GetMapping("/userDistribute")
    public String userDistribute(Map<String, Object> map, Integer userId){
        HnInformation informations = informationMapper.getInfomationByUserId(userId);
        if (null != informations) {
            map.put("informations",informations);
        } else {
            map.put("informations",new HnInformation());
        }
        return "users/userDistribute";
    }

    @GetMapping("/order/creat")
    public String orderQuery(Map<String, Object> map, Authentication authentication){
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("user",thisUser);
        return "order/creat";
    }
    @GetMapping("/order/list")
    public String orderList(Map<String, Object> map, Authentication authentication){
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("user",thisUser);
        return "order/orderList";
    }

    @GetMapping("/order/orderDetail")
    public String orderDetail(Map<String, Object> map, Authentication authentication,String orderNum){
        HnOrder dto = new HnOrder();
        dto.setOrderNum(orderNum);
        List<HnOrder> orderList = orderMapper.selectByOrder(dto);
        if (CollectionUtils.isNotEmpty(orderList)) {
            map.put("order",orderList.get(0));
        }else {
            HnOrder order = new HnOrder();
            map.put("order",order);
        }
        List<HnOrderInfo> orderInfoList = orderInfoMapper.selectByOrderNum(orderNum);
        if (CollectionUtils.isNotEmpty(orderList)) {
            HnOrderInfo orderInfo = orderInfoList.get(0);
            List<HnOrderPhoto> photoList = photoMapper.selectByInfoId(orderInfo.getOrderInfoId());
            orderInfo.setOrderPhotoList(photoList);
            map.put("orderInfo",orderInfo);
        }else {
            HnOrderInfo orderInfo = new HnOrderInfo();
            map.put("orderInfo",orderInfo);
        }
        return "order/orderDetail";
    }

    @GetMapping("/order/edit")
    public String orderEdit(Map<String, Object> map, Authentication authentication,String orderNum){
        HnOrder dto = new HnOrder();
        dto.setOrderNum(orderNum);
        List<HnOrder> orderList = orderMapper.selectByOrder(dto);
        if (CollectionUtils.isNotEmpty(orderList)) {
            map.put("order",orderList.get(0));
        }else {
            HnOrder order = new HnOrder();
            map.put("order",order);
        }
        List<HnOrderInfo> orderInfoList = orderInfoMapper.selectByOrderNum(orderNum);
        if (CollectionUtils.isNotEmpty(orderList)) {
            HnOrderInfo orderInfo = orderInfoList.get(0);
            List<HnOrderPhoto> photoList = photoMapper.selectByInfoId(orderInfo.getOrderInfoId());
            orderInfo.setOrderPhotoList(photoList);
            map.put("orderInfo",orderInfo);
        }else {
            HnOrderInfo orderInfo = new HnOrderInfo();
            map.put("orderInfo",orderInfo);
        }
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("user",thisUser);
        return "order/edit";
    }

    @GetMapping("/updatePassword")
    public String updatePassword(Map<String, Object> map, Authentication authentication) {
        String userName = authentication.getName();
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        map.put("user",thisUser);
        return "users/updatePassword";
    }

    @GetMapping("/assign/list")
    public String assignList(Map<String, Object> map, Authentication authentication){
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("user",thisUser);
        return "assign/assignList";
    }

    @GetMapping("chooseUser")
    public String chooseUser(Map<String, Object> map, Authentication authentication,@RequestParam List<Integer> ids){
        map.put("ids",ids);
        return "assign/chooseUser";
    }

    @GetMapping("/checkOrder/list")
    public String orderReceiveList(Map<String, Object> map, Authentication authentication){
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("user",thisUser);
        return "receive/orderList";
    }

    @GetMapping("/salary/settle")
    public String salarySettle(Map<String, Object> map, Authentication authentication){
        String userName = authentication.getName();
        List<HnInformation>  informations = informationMapper.getInfomationByUserName(userName);
        HnUser user = new HnUser();
        user.setUserName(userName);
        HnUser thisUser = userMapper.selectByUserName(user);
        List<HnOrder> orderList = orderMapper.selectByReceiveUserId(thisUser);
        if (CollectionUtils.isNotEmpty(informations)) {
            HnInformation information = informations.get(0);
            if (StringUtils.isNotBlank(information.getUserHeardImp())) {
                information.setShowImg(true);
            }else{
                information.setShowImg(false);
            }
            map.put("information",information);
        } else {
            HnInformation information = new HnInformation();
            information.setShowImg(false);
            map.put("information",information);
        }
        map.put("count",orderList.size());
        int salary = 0;
        for (int i = 0; i < orderList.size(); i++) {
            salary = salary + orderList.get(i).getOrderPrice();
        }
        map.put("salary",salary);
        map.put("user",thisUser);
        return "salary/detail";
    }

    @GetMapping("/salary/list")
    public String salaryList() {
        return "salary/userList";
    }

}
